package cn.com.dragonsoftbravo.utils.azure.factory;

import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlobDirectory;

import cn.com.dragonsoftbravo.utils.SystemConfigHolder;
import cn.com.dragonsoftbravo.utils.azure.AzureConstants;

public class BlobContainerFactory {

	private static String imageContainerName;
	private static String compaignDirectoryName;
	private static String alphaCafeDirectoryName;
	private static String thumbnailDirectoryName;
	private static String originalDirectoryName;

	private static CloudBlobContainer imageContainer;

	private static CloudBlobDirectory compaignDirectory;
	private static CloudBlobDirectory alphaCafeDirectory;
	private static CloudBlobDirectory thumbnailDirectory;
	private static CloudBlobDirectory originalDirectory;

	static {
		imageContainerName = SystemConfigHolder.instance().getValue(AzureConstants.CONTAINER_NAME);
		compaignDirectoryName = SystemConfigHolder.instance().getValue(AzureConstants.COMPAIGN_DIRECTORY_NAME);
		alphaCafeDirectoryName = SystemConfigHolder.instance().getValue(AzureConstants.ALPHA_CAFE_DIRECTORY_NAME);
		thumbnailDirectoryName = SystemConfigHolder.instance().getValue(AzureConstants.THUMBNAIL_DIRECTORY_NAME);
		originalDirectoryName = SystemConfigHolder.instance().getValue(AzureConstants.ORIGINAL_DIRECTORY_NAME);
	}

	public static CloudBlobContainer getContainer() throws Exception {
		if (imageContainer == null) {
			CloudBlobClient blobClient = BlobClientFactory.getCloudBlobClient();
			imageContainer = blobClient.getContainerReference(imageContainerName);
			imageContainer.createIfNotExists();
		}
		return imageContainer;
	}

	public static CloudBlobDirectory getDirectoryName(String campaignId, int fileType) throws Exception {
		String mediaType = (fileType == 0) ? "image" : "video";
		CloudBlobDirectory dir1 = getContainer().getDirectoryReference(campaignId);
		return dir1.getDirectoryReference(mediaType);
	}

	private static CloudBlobDirectory getCompaignDirectoryName() throws Exception {
		if (compaignDirectory == null) {
			compaignDirectory = getContainer().getDirectoryReference(compaignDirectoryName);
		}
		return compaignDirectory;
	}

	private static CloudBlobDirectory getAlphaCafeDirectory() throws Exception {
		if (alphaCafeDirectory == null) {
			alphaCafeDirectory = getCompaignDirectoryName().getDirectoryReference(alphaCafeDirectoryName);
		}

		return alphaCafeDirectory;
	}

	public static CloudBlobDirectory getOriginailDirectory() throws Exception {
		if (originalDirectory == null) {
			originalDirectory = getAlphaCafeDirectory().getDirectoryReference(originalDirectoryName);
		}
		return originalDirectory;
	}

	public static CloudBlobDirectory getThumbnailDirectory() throws Exception {
		if (thumbnailDirectory == null) {
			thumbnailDirectory = getAlphaCafeDirectory().getDirectoryReference(thumbnailDirectoryName);
		}
		return thumbnailDirectory;
	}

}
